package com.fc.h5dispaly.h5display.home.v;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fc.h5dispaly.h5display.util.Util;

import static android.content.Context.BIND_AUTO_CREATE;
import static com.fc.h5dispaly.h5display.home.v.HomeLayout.FOCUS__COLOR;
import static com.fc.h5dispaly.h5display.home.v.HomeLayout.TEXT_UN_FOCUS__COLOR;

/**
 * Created by fc on 2018/8/11.
 */

public class HomeImageLayout extends FrameLayout {
    private TextView imageView_1, imageView_2, imageView_3, imageView_4, imageView_5;
    private Context mContext;

    public HomeImageLayout(@NonNull Context context) {
        super(context);
        mContext = context;
        initView();

    }

    private void initView() {
        imageView_1 = new TextView(mContext);
        imageView_1.setOnFocusChangeListener(onFocusChangeListener);
        imageView_1.setFocusable(true);
        imageView_1.setText("第一个大块块");
        imageView_1.setTextColor(TEXT_UN_FOCUS__COLOR);
        imageView_1.setGravity(Gravity.CENTER);
        LayoutParams params = new FrameLayout.LayoutParams(Util.Div(930), Util.Div(620));
        params.topMargin = Util.Div(120);
        params.leftMargin = Util.Div(100);
        addView(imageView_1, params);
        imageView_1.setBackgroundColor(Color.WHITE);
        imageView_1.setAlpha(0.2f);
        imageView_1.requestFocus();
        imageView_1.setClickable(true);
        imageView_1.setOnClickListener(new OnClickListener() {
            @SuppressLint("ShowToast")
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext,"点击",Toast.LENGTH_SHORT).show();
                String action = "android.intent.action.bind.gameservice";
                Intent intent = new Intent(action);
                intent.setPackage("com.fc.h5dispaly.h5display");
               boolean s =   mContext.bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
                Log.i("FFFCCC","*********s: "+ s);
            }
        });

        imageView_2 = new TextView(mContext);
        imageView_2.setOnFocusChangeListener(onFocusChangeListener);
        imageView_2.setFocusable(true);
        imageView_2.setText("第二个块块");
        imageView_2.setTextColor(TEXT_UN_FOCUS__COLOR);
        imageView_2.setGravity(Gravity.CENTER);
        params = new FrameLayout.LayoutParams(Util.Div(445), Util.Div(200));
        params.topMargin = Util.Div(780);
        params.leftMargin = Util.Div(100);
        addView(imageView_2, params);
        imageView_2.setBackgroundColor(Color.WHITE);
        imageView_2.setAlpha(0.2f);
        imageView_2.setClickable(true);
        imageView_2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    Log.i("FFFCCC","Price: "+ getPrice("逃生2"));
                    Toast.makeText(mContext,"服务端的价格:"+ getPrice("逃生2"),Toast.LENGTH_SHORT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


        imageView_3 = new TextView(mContext);
        imageView_3.setOnFocusChangeListener(onFocusChangeListener);
        imageView_3.setFocusable(true);
        imageView_3.setTextColor(TEXT_UN_FOCUS__COLOR);
        imageView_3.setText("第三个块块");
        imageView_3.setGravity(Gravity.CENTER);
        params = new FrameLayout.LayoutParams(Util.Div(445), Util.Div(200));
        params.topMargin = Util.Div(780);
        params.leftMargin = Util.Div(580);
        addView(imageView_3, params);
        imageView_3.setBackgroundColor(Color.WHITE);
        imageView_3.setAlpha(0.2f);

        imageView_4 = new TextView(mContext);
        imageView_4.setOnFocusChangeListener(onFocusChangeListener);
        imageView_4.setFocusable(true);
        imageView_4.setText("第四个块块");
        imageView_4.setGravity(Gravity.CENTER);
        imageView_4.setTextColor(TEXT_UN_FOCUS__COLOR);
        params = new FrameLayout.LayoutParams(Util.Div(760), Util.Div(410));
        params.topMargin = Util.Div(120);
        params.leftMargin = Util.Div(1055);
        addView(imageView_4, params);
        imageView_4.setBackgroundColor(Color.WHITE);
        imageView_4.setAlpha(0.2f);

        imageView_5 = new TextView(mContext);
        imageView_5.setOnFocusChangeListener(onFocusChangeListener);
        imageView_5.setFocusable(true);
        imageView_5.setTextColor(TEXT_UN_FOCUS__COLOR);
        imageView_5.setText("第五个块块");
        imageView_5.setGravity(Gravity.CENTER);
        params = new FrameLayout.LayoutParams(Util.Div(760), Util.Div(410));
        params.topMargin = Util.Div(565);
        params.leftMargin = Util.Div(1055);
        addView(imageView_5, params);
        imageView_5.setBackgroundColor(Color.WHITE);
        imageView_5.setAlpha(0.2f);
    }

    private IBinder mRemote = null;
    private ServiceConnection mServiceConnection= new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mRemote = iBinder;
            Log.i("FFFCCC","绑定成功 ");
            Toast.makeText(mContext, "绑定成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mRemote = null;
            Toast.makeText(mContext, "远程服务链接已断", Toast.LENGTH_SHORT).show();
        }
    };

    private int getPrice(String name){
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        int result = 0;

        try {
            data.writeString(name);
            mRemote.transact(1,data,reply,0);
            result =reply.readInt();
        } catch (RemoteException e) {
            e.printStackTrace();
        }finally {
            reply.recycle();
            data.recycle();
        }
      return  result;

    }

    private View.OnFocusChangeListener onFocusChangeListener = new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b) {
                view.setBackgroundColor(FOCUS__COLOR);
                view.setAlpha(1f);
                ((TextView) view).setTextColor(Color.WHITE);
            } else {
                view.setBackgroundColor(Color.WHITE);
                view.setAlpha(0.2f);
                ((TextView) view).setTextColor(TEXT_UN_FOCUS__COLOR);
            }
        }
    };
}
